package com.xrui.hbase.conversion;


import com.google.common.base.Preconditions;
import com.moz.kiji.delegation.Lookups;
import com.moz.kiji.delegation.NamedProvider;
import com.xrui.hbase.HBaseCell;
import com.xrui.hbase.model.RecordFields;


import java.io.IOException;

/**
 * Interface for HBase cell converters.
 * <p>
 * A HBase cell converter is configured to convert precisely one specific cell
 * to a generic K/V mapping object {@link RecordFields}.
 * <p>
 * HBase cell converters are instantiated via {@link CellConverter.Factory}.
 * <p>
 * Intended for developers only.
 */
public interface CellConverter extends NamedProvider {
    /**
     * Converts a HBase cell to a generic field name and value mapping object {@link RecordFields}
     * @param cell the HBase cell to convert
     * @return the converted generic field name and value mapping object {@link RecordFields}
     * @throws IOException when failed to convert
     */
    RecordFields convert(HBaseCell cell) throws IOException;
    //HBaseCell revert(RecordFields recordFields);

    class Factory {
        /**
         * Creates a new converter with specified convert provider name
         * @param name of the converter
         * @return new instance of the converter
         */
        public static CellConverter create(String name) {
            Preconditions.checkNotNull(name);
            return Lookups.getNamed(CellConverter.class).lookup(name);
        }
    }
}
